/*
    This file is part of Slydini.

    Slydini is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Slydini is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Slydini.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "gesture.h"

vector<Gesture*> Gesture::gestures;

Gesture Gesture::down_right("22222222222222226666666666666666", "down_right");
Gesture Gesture::down_left("22222222222222224444444444444444", "down_left");
Gesture Gesture::square("6666666666666666222222222222222244444444444444448888888888888888", "square");
Gesture Gesture::right_left("66666666666666664444444444444444", "right_left");
Gesture Gesture::left_right("44444444444444446666666666666666", "left_right");
Gesture Gesture::circle("888999999666666333333222222111111444444777777888", "circle");
Gesture Gesture::n("888888888888888833333333333338888888888888888", "n");
Gesture Gesture::z("666666666666666111111111111111666666666666666", "z");
Gesture Gesture::up_down("88888888888888882222222222222222", "up_down");
Gesture Gesture::down_up("22222222222222228888888888888888", "down_up");
Gesture Gesture::up_left("88888888888888884444444444444444", "up_left");
Gesture Gesture::up_right("88888888888888886666666666666666", "up_right");
Gesture Gesture::up_arrow("99999999999999993333333333333333", "up_arrow");
Gesture Gesture::down_arrow("33333333333333339999999999999999", "down_arrow");
Gesture Gesture::left_arrow("11111111111111113333333333333333", "left_arrow");
Gesture Gesture::right_arrow("33333333333333331111111111111111", "right_arrow");
Gesture Gesture::hourglass("666666666666666666111111111111111666666666666666666777777777777777", "hourglass");
Gesture Gesture::snail("8888444444442222222266666666666688888888888844444444444444442222222222222222", "snail");
Gesture Gesture::up_parabola("6666668888888889999663333222222222666666", "up_parabola");
Gesture Gesture::down_parabola("6666662222222223333669999888888888666666", "down_parabola");
Gesture Gesture::flag("8888888888888888666666666662222222222244444444444", "flag");
Gesture Gesture::m("8888888888888888333333333399999999992222222222222222", "m");
Gesture Gesture::triangle("99999999999999999333333333333333334444444444444", "triangle");
Gesture Gesture::down_right_cross("222222222222222777777777666666666666666", "down_right_cross");
Gesture Gesture::down_left_cross("222222222222222999999999444444444444444", "down_left_cross");
Gesture Gesture::up_right_cross("888888888888888811111116666666666666666", "up_right_cross");

Gesture::Gesture(string name, string sequence) : name(name), sequence(sequence)  {
	gestures.push_back(this);
}

string Gesture::getName() {
	return name;
}

string Gesture::getSequence() {
	return sequence;
}

vector<Gesture*> Gesture::getGestures() {
	return gestures;
}

Gesture *Gesture::getGesture(string name) {
	for (vector<Gesture*>::iterator I = gestures.begin(); I != gestures.end(); I++)
		if ((*I)->getName() == name)
			return *I;
	return NULL;
}

bool Gesture::operator==(const Gesture &A) {

	return false;
}

int Gesture::minim(int a, int b) {
        return a<b?a:b;
}

int Gesture::getDistance(string second) {
	vector< vector<int> > d;
	d.resize(sequence.length()+1);
	for (unsigned int i=0;i<d.size();i++)
		d[i].resize(second.length()+1);

	int cost;

	for (unsigned int i=0;i<=sequence.length();i++) {
		d[i][0]=i;
	}
	for (unsigned int j=0;j<=second.length();j++) {
		d[0][j]=j;
	}

	for (unsigned int i=1;i<=sequence.length();i++) {
		for (unsigned int j=1;j<=second.length();j++) {
			if (sequence[i-1]==second[j-1]) {
				cost=0;
			} else {
				cost=1;
			}
			d[i][j]=minim(minim(d[i-1][j]+1,d[i][j-1]+1),d[i-1][j-1]+cost);
		}
	}
	return d[sequence.length()][second.length()];
}
